/*
 * @author jingsong.chen, QQ:77132995, email:kazeik@163.com
 * 2020-03-05 12:56
 * 类说明:
 */
import 'package:image_scan_crop/utils/Utils.dart';
import 'package:quiver/strings.dart';

class TimeUtils {
  //"2019-06-20 15:32:41"
  static String diffStrTime(String formatTime) {
    DateTime date = DateTime.parse(formatTime);
    return diffTime(date);
  }

  static String diffTime(DateTime daynow) {
    Duration chaf = DateTime.now().difference(daynow);
    if (chaf.inDays != 0) {
      if (chaf.inDays > 30) {
        return "${daynow.year}-${daynow.month}-${daynow.day}";
      } else
        return "${chaf.inDays}天前";
    } else if (chaf.inHours != 0) {
      return "${chaf.inHours}小时前";
    } else if (chaf.inMinutes != 0) {
      return "${chaf.inMinutes}分钟前";
    } else if (chaf.inSeconds != 0) {
      return "${chaf.inSeconds}刚刚";
    } else {
      return "";
    }
  }

  static int checkLastDayByMonth(dynamic month) {
    int tempMonth = 1;
    if (month is String) {
      tempMonth = int.parse(month);
    } else {
      tempMonth = month;
    }

    if (tempMonth == 0) {
      return 1;
    }
    switch (tempMonth) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        tempMonth = 31;
        break;
      case 2:
        tempMonth = 28;
        break;
      case 4:
      case 6:
      case 9:
      case 11:
        tempMonth = 30;
        break;
    }
    return tempMonth;
  }

  static String? getCurrTime(DateTime daynow, {TimeTypes? format}) {
    if (null == format || format == TimeTypes.YYYY_MM_DD_HH_MM_SS) {
      return "${daynow.year}-${TimeUtils.checkTime(daynow.month)}-${TimeUtils.checkTime(daynow.day)} ${TimeUtils.checkTime(daynow.hour)}:${TimeUtils.checkTime(daynow.minute)}:${TimeUtils.checkTime(daynow.second)}";
    } else if (format == TimeTypes.YYYY_MM_DD) {
      return "${daynow.year}-${TimeUtils.checkTime(daynow.month)}-${TimeUtils.checkTime(daynow.day)}";
    } else if (format == TimeTypes.YYYY_MM) {
      return "${daynow.year}-${TimeUtils.checkTime(daynow.month)}";
    }
  }

  static DateTime getNextDate({String? formatTime}) {
    DateTime _dateTime;
    if (isEmpty(formatTime)) {
      _dateTime = DateTime.now();
    } else {
      _dateTime = DateTime.parse(formatTime!);
    }
    int _tempTime = _dateTime.millisecondsSinceEpoch + (1000 * 60 * 60 * 24);
    return DateTime.fromMillisecondsSinceEpoch(_tempTime);
  }

  static String formtDateTime(String timeStr, {TimeTypes? format}) {
    String res = timeStr.split('(').last;
    res = res.split(')').first;
    if (res.contains('-')) {
      return '时间格式错误';
    }
    var date1 = DateTime.fromMillisecondsSinceEpoch(int.parse(res));
    // var date2 = new DateTime.fromMicrosecondsSinceEpoch(int.parse(res) * 1000);
    res = date1.toString();

    if (res.contains('.')) {
      res = res.split('.').first;
    }
    DateTime time = DateTime.parse(res);
    return getCurrTime(time, format: format)!;
  }

  static String getTimeByType(TimeTypes format, {DateTime? dayTime}) {
    assert(format != null);
    if (dayTime == null) {
      return getCurrTime(DateTime.now(), format: format)!;
    } else {
      return getCurrTime(dayTime, format: format)!;
    }
  }

  static String formatUtcTime(String utcTime) {
    if (isEmpty(utcTime)) return "";
    bool has = utcTime.contains("T");
    if (has) {
      DateTime? _time = DateTime.tryParse(utcTime);
      return getCurrTime(_time!)!;
    } else {
      return Utils.checkValue(utcTime);
    }
  }

  static String getLastDayOfMonth() {
    int year = DateTime.now().year;
    int month = DateTime.now().month;
    int lastDay = checkLastDayByMonth(month);
    return "$year-${checkTime(month)}-$lastDay";
  }

  static String getFristDayOfMonth() {
    int year = DateTime.now().year;
    int month = DateTime.now().month;
    return "$year-${checkTime(month)}-01";
  }

  static String getTimeByMilliseconds(int time, {TimeTypes? format}) {
    var daynow = DateTime.fromMillisecondsSinceEpoch(time * 1000);
    return getCurrTime(daynow, format: format)!;
  }

  static String getTimeBySeconds(int time, {TimeTypes? format}) {
    var daynow = DateTime.fromMillisecondsSinceEpoch(time);
    return getCurrTime(daynow, format: format)!;
  }

  static String getTime(int time) {
    var daynow = DateTime.fromMillisecondsSinceEpoch(time * 1000);
    return diffTime(daynow);
  }

  static String checkTime(int time) {
    return time < 10 ? "0$time" : "$time";
  }

  static String formatTime(int time) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(time * 1000);
    return "${dateTime.year}/${checkTime(dateTime.month)}/${checkTime(dateTime.day)}";
  }
}

enum TimeTypes { YYYY_MM_DD, YYYY_MM_DD_HH_MM_SS, YYYY_MM }
